package ru.ys.jsolver.view;


import javax.swing.*;

import sun.java2d.ScreenUpdateManager;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SizeRequest extends JFrame {
	private Dimension myDim;
	private JTextField myText1;
	private JTextField myText2;
	
	
	
	private Container putLeft(Container c) {
		JPanel tempPanel = new JPanel();			
		BoxLayout box = new BoxLayout(tempPanel, BoxLayout.X_AXIS);		
		tempPanel.setLayout(box);		
		tempPanel.add(c);
		tempPanel.add(Box.createGlue());
		tempPanel.add(Box.createHorizontalStrut(5));
		return tempPanel;
	}
	
	private Container putRight(Container c) {
		JPanel tempPanel = new JPanel();			
		BoxLayout box = new BoxLayout(tempPanel, BoxLayout.X_AXIS);		
		tempPanel.setLayout(box);		
		tempPanel.add(Box.createGlue());
		tempPanel.add(c);		
		return tempPanel;
	}
	
	private void okPressed() {
		synchronized (myDim) {
			try {
				myDim.width = Integer.parseInt(myText1.getText());
				myDim.height = Integer.parseInt(myText2.getText());
				myDim.notify();
			} catch (NumberFormatException e) {
				return;
			}
			dispose();
		}
	}
	
	private void cancelPressed() {
		synchronized (myDim) {
			myDim.height = -1;
			myDim.width = -1;
			myDim.notify();
		}
	}
	
	SizeRequest(Dimension dim) {		
		super();
		myDim = dim;
		setTitle("Input size of board");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);	
		JPanel loginPanel = new JPanel();
		GridLayout gl = new GridLayout(2, 0, 5, 12);
		loginPanel.setLayout(gl);
		
		loginPanel.add(putRight(new JLabel("Width")));
		myText1 = new JTextField("10");
		loginPanel.add(putLeft(myText1));
		myText2 = new JTextField("10");
		loginPanel.add(putRight(new JLabel("Height")));
		loginPanel.add(putLeft(myText2));
		
	
		JPanel buttonPanel = new JPanel();
		GridLayout buttonBox = new GridLayout(0, 2, 12, 5);
		
		JButton okButton = new JButton("OK");
		JButton cancelButton = new JButton("Cancel");
		
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okPressed();
			}});
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelPressed();				
			}
			
		});
		
		buttonPanel.setLayout(buttonBox);
		buttonPanel.add(okButton);		
		buttonPanel.add(cancelButton);
		
		JPanel mainPanel = new JPanel();
		BoxLayout mainBox = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(mainBox);
		mainPanel.add(loginPanel);
		mainPanel.add(Box.createVerticalStrut(5));
		mainPanel.add(buttonPanel);
		
		mainPanel.revalidate();
		getContentPane().add(mainPanel);
		setResizable(false);		
		System.out.println(getBounds());
		//setLocation(new Point(100, 100));
		
		pack();
		setVisible(true);
	}

}